package com.shop.web;

import com.shop.entity.PageBean;
import com.shop.entity.Product;
import com.shop.service.GoodsService;
import com.shop.service.impl.GoodServiceImpl;
import com.shop.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author WW
 * @Date 2020/7/13 19:58
 */
@WebServlet("/product")
public class ProductController extends BaseServlet {
    //展示
    public String show(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        //获取关键字
        String matter = null;
        String  id=null;
         matter = req.getParameter("matter");
          id = req.getParameter("tid");
        //创建Service
        GoodsService goodsService = new GoodServiceImpl();
        Integer tid =null;
        if(id !=null && id.trim() != ""){
            tid =Integer.parseInt(id);
        }
        //每页的大小
        int pageSize = 8;
        //将当前页面转换成int类型
        int currentPage = 1;
         //当前页的数据
        List<Product> lis = null;
        try {
            //获取当前页码
            String page = req.getParameter("currentPage");
            currentPage = Integer.parseInt(page);
        } catch (Exception e) {}

        if(matter!=null && matter.trim() != "" ) {
            Long i = 1l;
            Long totalCount = goodsService.count(matter);
            if (totalCount%pageSize == 0) {
                i = totalCount/pageSize;
            }else {
                i = totalCount/pageSize + 1 ;
            }
            List<Product> list = goodsService.list(matter,currentPage,pageSize);
            PageBean<Product> pageBean = new PageBean(list,currentPage,pageSize,totalCount);
            pageBean.setTotalPage(Math.toIntExact(i));
            req.setAttribute("pageBean",pageBean);
        }else if(tid != null){
            Long i = 1l;
            Long totalCount = goodsService.count(tid);
            if (totalCount%pageSize == 0) {
                i = totalCount/pageSize;
            }else {
                i = totalCount/pageSize + 1 ;
            }
            //获取总记录数
            List<Product> list = goodsService.list(tid, currentPage, pageSize);
            PageBean<Product> pageBean = new PageBean(list, currentPage, pageSize, totalCount);
            pageBean.setTotalPage(Math.toIntExact(i));
            req.setAttribute("pageBean", pageBean);
            }
        return Constants.FORWARD + "/pages/client/goodsList.jsp";
    }

    //详细
    public void detail(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        GoodServiceImpl goodService = new GoodServiceImpl();
        Integer pid = Integer.parseInt(req.getParameter("pid"));
        Product product = goodService.listone(pid);

        req.setAttribute("product",product);
        req.getRequestDispatcher("/pages/client/goodsDetail.jsp").forward(req,resp);
    }

}
